package com.kaili.basic.controller.web.v1;

import com.baomidou.mybatisplus.plugins.Page;
import com.kaili.basic.pojo.dto.BaseSupplyAddDTO;
import com.kaili.basic.pojo.dto.BaseSupplyQueryDTO;
import com.kaili.basic.pojo.entity.BaseSupply;
import com.kaili.basic.service.BaseSupplyService;
import com.kaili.basic.version.ApiVersion;
import com.kaili.common.response.CommonStatus;
import com.kaili.common.response.RestStatus;
import com.kaili.common.util.LoginDetailUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.hibernate.validator.constraints.NotEmpty;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;
import java.util.List;
import java.util.Objects;

@RestController
@ApiVersion(1)
@Validated
@RequestMapping("/{api_version}")
@Api(tags = "货源管理", description = "货源管理相关API")
public class BaseSupplyController {

    @Autowired
    private BaseSupplyService baseSupplyService;

    @PostMapping("/supply")
    @ApiOperation(value = "新增货源")
    public RestStatus addSupply(@Validated @RequestBody BaseSupplyAddDTO supplyAddDTO) {
        BaseSupply baseSupply = new BaseSupply();
        BeanUtils.copyProperties(supplyAddDTO, baseSupply);
        List<BaseSupply> list = baseSupplyService.getSupplyByNameAndContacts(baseSupply);
        if (list.isEmpty()) {
            String result = baseSupplyService.getMaxSupplyCode();
            if (StringUtils.isEmpty(result)) {
                result = "0000";
            }else {
                Integer code = Integer.valueOf(result) + 1;
                switch (code.toString().length()) {
                    case 1 :
                        result = "000" + code.toString();
                        break;
                    case 2 :
                        result = "00" + code.toString();
                        break;
                    case 3 :
                        result = "0" + code.toString();
                        break;
                    case 4 :
                        result = code.toString();
                        break;
                    default :
                        result = "-1";
                        break;
                }
            }
            baseSupply.setSupplyCode(result);
            baseSupplyService.addSupply(baseSupply);
            return CommonStatus.ADD_OK;
        }
        return CommonStatus.ADD_DEFEAT;
    }

    @DeleteMapping("/supply/{supplyId}")
    @ApiOperation(value = "根据Id删除货源")
    public RestStatus deleteSupply(@NotNull(message = "货源ID不能为空") @PathVariable("supplyId") Long supplyId) {
        if (supplyId == null) {
            return CommonStatus.BAD_REQUEST;
        }
        baseSupplyService.deleteSupply(supplyId);
        return CommonStatus.DELETE_OK;
    }

    @PutMapping("/supply")
    @ApiOperation(value = "更新货源")
    public RestStatus updateSupply(@Validated @RequestBody BaseSupplyAddDTO supplyAddDTO) {
        if (supplyAddDTO == null) {
            return CommonStatus.ADD_DEFEAT;
        }
        BaseSupply baseSupply = new BaseSupply();
        BeanUtils.copyProperties(supplyAddDTO, baseSupply);
        List<BaseSupply> list = baseSupplyService.getSupplyByNameAndContacts(baseSupply);
        for (BaseSupply b : list){
            if (!Objects.equals(b.getSupplyId(), baseSupply.getSupplyId())){
                return CommonStatus.ADD_DEFEAT_SUPPLY;
            }
        }
        baseSupplyService.updateSupply(baseSupply);
        return CommonStatus.UPDATE_OK;
    }

    @GetMapping("/supply/{supplyName}")
    @ApiOperation(value = "根据货源公司模糊查询")
    public List<BaseSupply> getSupplyLikeName(@NotEmpty(message = "货源公司不能为空") @PathVariable("supplyName") String supplyName){
        return baseSupplyService.getSupplyLikeName(supplyName);
    }

    @GetMapping("/supply")
    @ApiOperation(value = "分页查询货源")
    public Page<BaseSupply> querySupply(@ModelAttribute BaseSupplyQueryDTO supplyQueryDTO) {
        BaseSupply baseSupply = new BaseSupply();
        BeanUtils.copyProperties(supplyQueryDTO, baseSupply);
        if(LoginDetailUtils.getSiteId()==1)
            baseSupply.setSiteId(null);
        return baseSupplyService.querySupply(new Page<BaseSupply>(baseSupply.getPage(), baseSupply.getPageSize()), baseSupply);
    }

    @GetMapping("/supply/code")
    @ApiOperation(value = "获取最新货源编号")
    public String getSupplyCode() {
        String str = baseSupplyService.getMaxSupplyCode();
        if (StringUtils.isEmpty(str)) {
            str = "0000";
        }
        String result = String.valueOf(Integer.valueOf(str)+1);
        if (result.length() == 1){
            result = "000" + result;
        }else if (result.length() == 2){
            result = "00" + result;
        }else if (result.length() == 3){
            result = "0" + result;
        }
        return result;
    }
}